/**
 * Created by liuqingxia on 2016/6/28.
 */

var sourceButtons = genSmallUseBtn("datasource-use-btn sourceDataSourceUseBtn", "right");
var targetButtons = genSmallUseBtn("datasource-use-btn targetDataSourceUseBtn", "right");

var sourceTableNameUlBtnId = "sourceTableNameUiBtn";
var sourceTableNameLiPrefix = "source";

var sourceTbNameInputId = "sourceSearchTableNameInput";
var sourceRelatedTbNameDivId = "sourceRelatedTableNamesDiv";
var sourceRelatedTbNameUlId = "sourceRelatedTableNamesUl";

var isRocketMqTarget = 0;//target是否为rocketMq类型
var sourceDataSourceId;//源orcale数据源Id
var targetDataSourceId;//目标数据源Id
var tableName;//oracle表名

//提示语
var paramsCommentMap = {
    sourceDataSourceId: "请选择source数据源",
    targetDataSourceId: "请选择target数据源",
    tableName: "请输入或选择表名",
    projectName: "请选择项目组",
    businessKey: "请输入Gcache业务key",
    dataStructure: "请选择redis命令",
    vdataStructure: "请选择值类型（Map或String）",
    targetKey: "请输入数据项key",
    targetSeparator: "请输入分隔符",
    targetValue: "请输入字段对应关系",
    moveMode: "请选择迁移模式"
};

/**
 * main入口
 */
$(document).ready(function () {
    changeSourceDataSourceTypeSelect();
    showDataSourceTree("oracle", "sourceDataSourceTreeDiv", sourceButtons);
    showDataSourceTree("mysql", "targetDataSourceTreeDiv", targetButtons);

    clickDataSourceUseBtn("source");
    clickDataSourceUseBtn("target");
    clickTableNameLi("source", sourceTableNameUlBtnId);

    clickSearchTableNameBtn("sourceSearchTableBtn", sourceTbNameInputId, sourceTableNameLiPrefix);

    searchTableNameInputChange(sourceTbNameInputId, sourceTableNameLiPrefix, sourceRelatedTbNameDivId, sourceRelatedTbNameUlId);

    clickRelatedTableNameLi(sourceTableNameLiPrefix, sourceRelatedTbNameDivId);

    //redisCmdTypeChange();

    clickInsertBtn();
});

function changeSourceDataSourceTypeSelect() {
    $(document).on("change", "select[name='targetDataSourceSelect']", function () {
        var type = $.trim($(this).val());
        var rocketmqTemplateDiv = $("#rocketmqTemplateDiv");
        var targetDataSourceDiv = $("#targetDataSourceDiv");
        if (type == "rocketmq") {
        	isRocketMqTarget = 1;
        	rocketmqTemplateDiv.show();
            targetDataSourceDiv.hide();
        } else {
        	isRocketMqTarget = 0;
        	rocketmqTemplateDiv.hide();
            targetDataSourceDiv.show();
            showDataSourceTree(type, "targetDataSourceTreeDiv", targetButtons);
        }
    });
}

/**
 * 改变redis数据类型
 */
function redisCmdTypeChange() {
    $(document).on("change", "select[id='dataStructure']", function () {
        var dataStruce = $.trim($(this).val());
        var vdataStructureDiv = $("#vdataStructureDiv");
        if (dataStruce == "LPUSH") {
            vdataStructureDiv.show();
        } else {
            vdataStructureDiv.hide();
        }
        // console.log("dataStruce: " + dataStruce);
    });
}

/**
 * 点击"使用"按钮
 * @param direction
 */
function clickDataSourceUseBtn(direction) {

    direction = $.trim(direction);

    $(document).on("click", "." + direction + "DataSourceUseBtn", function () {

        var dataSourceId = getCurrentDataSourceId($(this));
        if (direction == "source") {
            sourceDataSourceId = dataSourceId;
        } else if (direction == "target") {
            targetDataSourceId = dataSourceId;
        }

        var params = {};
        params['sourceId'] = dataSourceId;

        if (direction == "source") {
            params['visualContainerId'] = "sourceTableNameSelectDiv";
            params['tableNameLiClass'] = "sourceTableNameLi";
            params['tableNameUiButtonId'] = sourceTableNameUlBtnId;
            showTableNameSelect(params);
        } else if (direction == "target") {

        } else {

        }
    });
}

/**
 * 点击保存任务按钮
 */
function clickInsertBtn() {
    $(document).on("click", "#insertBtn", function () {
        var params = assembleParams();
        // console.log(JSON.stringify(params));
        if (!validateParams(params)) {

        }
        else {
            sendParams(params);
        }
    });
}

/**
 * 发送请求参数
 * @param params
 */
function sendParams(params) {
    $.postHttpSync(params, getUrlBySuffix("/yugong/task/insert"), function (json) {
        // console.log(JSON.stringify(json));
        if (json && json.success) {
            swal("提示", json['message'], "success");
        } else {
            swal("提示", json["message"], "error");
        }
    });
}


/**
 * 组装后台请求参数
 */
function assembleParams() {

    var params = {};
    params['sourceDataSourceId'] = sourceDataSourceId;
    params['tableName'] = getTableName();
    params['moveMode'] = $("#moveMode").val();
    params['projectName'] = getProjectName();

    params['isRocketMqTarget'] = isRocketMqTarget;
    if (isRocketMqTarget == 0) {
        params['targetDataSourceId'] = targetDataSourceId;
    } else {
        $.extend(params, getRocketMqParams());
    }

    if (params['isGcacheTarget'] == 1) {
        params['targetDataSourceId'] == "";
    }

    return params;

}

/**
 * 验证参数
 * @param params
 * @returns {boolean}
 */
function validateParams(params) {

    for (var propName in params) {
        var propValue = params[propName];
        if (isNull(propValue)) {
            var comment = paramsCommentMap[propName];
            if (comment != "undefined") {
                swal("提示", comment, "error");
                return false;
            }
        }
    }
    return true;
}

/**
 * 获取Gcache相关的参数
 * @returns {{}}
 */
function getRocketMqParams() {

    var params = {};
    params['topicName'] = $("#topicname").val();

    params['nameSrvAddr'] = $("#namesrvaddr").val();
    return params;

}

/**
 * 获取项目组名
 * @returns {string|*}
 */
function getProjectName() {
    return $.trim($("#projectName").val());
}


/**
 * 获取oracle表名
 * @returns {*}
 */
function getTableName() {
    var tableNameUlBtn = $("#" + sourceTableNameUlBtnId);
    return tableNameUlBtn.attr("title");
}


